home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / LANG / SCHEME / GNU / SCM4E1 / !Scm / slib / synrul < prev    next >
Text File  |  1992-10-19  |  10KB  |  329 lines

  1. ;;; -*-Scheme-*-
  2. ;;;
  3. ;;; Copyright (c) 1989-91 Massachusetts Institute of Technology
  4. ;;;
  5. ;;; This material was developed by the Scheme project at the
  6. ;;; Massachusetts Institute of Technology, Department of Electrical
  7. ;;; Engineering and Computer Science.  Permission to copy this
  8. ;;; software, to redistribute it, and to use it for any purpose is
  9. ;;; granted, subject to the following restrictions and understandings.
  10. ;;;
  11. ;;; 1. Any copy made of this software must include this copyright
  12. ;;; notice in full.
  13. ;;;
  14. ;;; 2. Users of this software agree to make their best efforts (a) to
  15. ;;; return to the MIT Scheme project any improvements or extensions
  16. ;;; that they make, so that these may be included in future releases;
  17. ;;; and (b) to inform MIT of noteworthy uses of this software.
  18. ;;;
  19. ;;; 3. All materials developed as a consequence of the use of this
  20. ;;; software shall duly acknowledge such use, in accordance with the
  21. ;;; usual standards of acknowledging credit in academic research.
  22. ;;;
  23. ;;; 4. MIT has made no warrantee or representation that the operation
  24. ;;; of this software will be error-free, and MIT is under no
  25. ;;; obligation to provide any services, by way of maintenance, update,
  26. ;;; or otherwise.
  27. ;;;
  28. ;;; 5. In conjunction with products arising from the use of this
  29. ;;; material, there shall be no use of the name of the Massachusetts
  30. ;;; Institute of Technology nor of any adaptation thereof in any
  31. ;;; advertising, promotional, or sales literature without prior
  32. ;;; written consent from MIT in each case.
  33.  
  34. ;;;; Rule-based Syntactic Expanders
  35.  
  36. ;;; See "Syntactic Extensions in the Programming Language Lisp", by
  37. ;;; Eugene Kohlbecker, Ph.D. dissertation, Indiana University, 1986.
  38. ;;; See also "Macros That Work", by William Clinger and Jonathan Rees
  39. ;;; (reference? POPL?).  This implementation is derived from an
  40. ;;; implementation by Kent Dybvig, and includes some ideas from
  41. ;;; another implementation by Jonathan Rees.
  42.  
  43. ;;; The expansion of SYNTAX-RULES references the following keywords:
  44. ;;;   ER-TRANSFORMER LAMBDA IF BEGIN SET! QUOTE
  45. ;;; and the following procedures:
  46. ;;;   CAR CDR NULL? PAIR? EQUAL? MAP LIST CONS APPEND
  47. ;;;   ILL-FORMED-SYNTAX
  48. ;;; it also uses the anonymous keyword SYNTAX-QUOTE.
  49.  
  50. ;;; For testing.
  51. ;;;(define (run-sr form)
  52. ;;;  (expand/syntax-rules form (lambda (x) x) eq?))
  53.  
  54. (define (make-syntax-rules-macrology)
  55.   (make-er-expander-macrology
  56.    (lambda (define-classifier base-environment)
  57.      base-environment            ;ignore
  58.      (define-classifier 'SYNTAX-RULES expand/syntax-rules))))
  59.  
  60. (define (expand/syntax-rules form rename compare)
  61.   (if (syntax-match? '((* IDENTIFIER) + ((IDENTIFIER . DATUM) EXPRESSION))
  62.              (cdr form))
  63.       (let ((keywords (cadr form))
  64.         (clauses (cddr form)))
  65.     (if (let loop ((keywords keywords))
  66.           (and (pair? keywords)
  67.            (or (memq (car keywords) (cdr keywords))
  68.                (loop (cdr keywords)))))
  69.         (syntax-error "keywords list contains duplicates" keywords)
  70.         (let ((r-form (rename 'FORM))
  71.           (r-rename (rename 'RENAME))
  72.           (r-compare (rename 'COMPARE)))
  73.           `(,(rename 'ER-TRANSFORMER)
  74.         (,(rename 'LAMBDA)
  75.          (,r-form ,r-rename ,r-compare)
  76.          ,(let loop ((clauses clauses))
  77.             (if (null? clauses)
  78.             `(,(rename 'ILL-FORMED-SYNTAX) ,r-form)
  79.             (let ((pattern (caar clauses)))
  80.               (let ((sids
  81.                  (parse-pattern rename compare keywords
  82.                         pattern r-form)))
  83.                 `(,(rename 'IF)
  84.                   ,(generate-match rename compare keywords
  85.                            r-rename r-compare
  86.                            pattern r-form)
  87.                   ,(generate-output rename compare r-rename
  88.                         sids (cadar clauses)
  89.                         syntax-error)
  90.                   ,(loop (cdr clauses))))))))))))
  91.       (ill-formed-syntax form)))
  92.  
  93. (define (parse-pattern rename compare keywords pattern expression)
  94.   (let loop
  95.       ((pattern pattern)
  96.        (expression expression)
  97.        (sids '())
  98.        (control #f))
  99.     (cond ((identifier? pattern)
  100.        (if (memq pattern keywords)
  101.            sids
  102.            (cons (make-sid pattern expression control) sids)))
  103.       ((and (or (zero-or-more? pattern rename compare)
  104.             (at-least-one? pattern rename compare))
  105.         (null? (cddr pattern)))
  106.        (let ((variable ((make-name-generator) 'CONTROL)))
  107.          (loop (car pattern)
  108.            variable
  109.            sids
  110.            (make-sid variable expression control))))
  111.       ((pair? pattern)
  112.        (loop (car pattern)
  113.          `(,(rename 'CAR) ,expression)
  114.          (loop (cdr pattern)
  115.                `(,(rename 'CDR) ,expression)
  116.                sids
  117.                control)
  118.          control))
  119.       (else sids))))
  120.  
  121. (define (generate-match rename compare keywords r-rename r-compare
  122.             pattern expression)
  123.   (letrec
  124.       ((loop
  125.     (lambda (pattern expression)
  126.       (cond ((identifier? pattern)
  127.          (if (memq pattern keywords)
  128.              (let ((temp (rename 'TEMP)))
  129.                `((,(rename 'LAMBDA)
  130.               (,temp)
  131.               (,(rename 'IF)
  132.                (,(rename 'IDENTIFIER?) ,temp)
  133.                (,r-compare ,temp
  134.                        (,r-rename ,(syntax-quote pattern)))
  135.                #f))
  136.              ,expression))
  137.              `#t))
  138.         ((and (zero-or-more? pattern rename compare)
  139.               (null? (cddr pattern)))
  140.          (do-list (car pattern) expression))
  141.         ((and (at-least-one? pattern rename compare)
  142.               (null? (cddr pattern)))
  143.          `(,(rename 'IF) (,(rename 'NULL?) ,expression)
  144.                  #F
  145.                  ,(do-list (car pattern) expression)))
  146.         ((pair? pattern)
  147.          (let ((generate-pair
  148.             (lambda (expression)
  149.               (conjunction
  150.                `(,(rename 'PAIR?) ,expression)
  151.                (conjunction
  152.                 (loop (car pattern)
  153.                   `(,(rename 'CAR) ,expression))
  154.                 (loop (cdr pattern)
  155.                   `(,(rename 'CDR) ,expression)))))))
  156.            (if (identifier? expression)
  157.                (generate-pair expression)
  158.                (let ((temp (rename 'TEMP)))
  159.              `((,(rename 'LAMBDA) (,temp) ,(generate-pair temp))
  160.                ,expression)))))
  161.         ((null? pattern)
  162.          `(,(rename 'NULL?) ,expression))
  163.         (else
  164.          `(,(rename 'EQUAL?) ,expression
  165.                      (,(rename 'QUOTE) ,pattern))))))
  166.        (do-list
  167.     (lambda (pattern expression)
  168.       (let ((r-loop (rename 'LOOP))
  169.         (r-l (rename 'L))
  170.         (r-lambda (rename 'LAMBDA)))
  171.         `(((,r-lambda
  172.         (,r-loop)
  173.         (,(rename 'BEGIN)
  174.          (,(rename 'SET!)
  175.           ,r-loop
  176.           (,r-lambda
  177.            (,r-l)
  178.            (,(rename 'IF)
  179.             (,(rename 'NULL?) ,r-l)
  180.             #T
  181.             ,(conjunction
  182.               `(,(rename 'PAIR?) ,r-l)
  183.               (conjunction (loop pattern `(,(rename 'CAR) ,r-l))
  184.                    `(,r-loop (,(rename 'CDR) ,r-l)))))))
  185.          ,r-loop))
  186.            #F)
  187.           ,expression))))
  188.        (conjunction
  189.     (lambda (predicate consequent)
  190.       (cond ((eq? predicate #T) consequent)
  191.         ((eq? consequent #T) predicate)
  192.         (else `(,(rename 'IF) ,predicate ,consequent #F))))))
  193.     (loop pattern expression)))
  194.  
  195. (define (generate-output rename compare r-rename sids template syntax-error)
  196.   (let loop ((template template) (ellipses '()))
  197.     (cond ((identifier? template)
  198.        (let ((sid
  199.           (let loop ((sids sids))
  200.             (and (not (null? sids))
  201.              (if (eq? (sid-name (car sids)) template)
  202.                  (car sids)
  203.                  (loop (cdr sids)))))))
  204.          (if sid
  205.          (begin
  206.            (add-control! sid ellipses syntax-error)
  207.            (sid-expression sid))
  208.          `(,r-rename ,(syntax-quote template)))))
  209.       ((or (zero-or-more? template rename compare)
  210.            (at-least-one? template rename compare))
  211.        (optimized-append rename compare
  212.                  (let ((ellipsis (make-ellipsis '())))
  213.                    (generate-ellipsis rename
  214.                           ellipsis
  215.                           (loop (car template)
  216.                             (cons ellipsis
  217.                                   ellipses))))
  218.                  (loop (cddr template) ellipses)))
  219.       ((pair? template)
  220.        (optimized-cons rename compare
  221.                (loop (car template) ellipses)
  222.                (loop (cdr template) ellipses)))
  223.       (else
  224.        `(,(rename 'QUOTE) ,template)))))
  225.  
  226. (define (add-control! sid ellipses syntax-error)
  227.   (let loop ((sid sid) (ellipses ellipses))
  228.     (let ((control (sid-control sid)))
  229.       (cond (control
  230.          (if (null? ellipses)
  231.          (syntax-error "missing ellipsis in expansion" #f)
  232.          (let ((sids (ellipsis-sids (car ellipses))))
  233.            (cond ((not (memq control sids))
  234.               (set-ellipsis-sids! (car ellipses)
  235.                           (cons control sids)))
  236.              ((not (eq? control (car sids)))
  237.               (syntax-error "illegal control/ellipsis combination"
  238.                     control sids)))))
  239.          (loop control (cdr ellipses)))
  240.         ((not (null? ellipses))
  241.          (syntax-error "extra ellipsis in expansion" #f))))))
  242.  
  243. (define (generate-ellipsis rename ellipsis body)
  244.   (let ((sids (ellipsis-sids ellipsis)))
  245.     (let ((name (sid-name (car sids)))
  246.       (expression (sid-expression (car sids))))
  247.       (cond ((and (null? (cdr sids))
  248.           (eq? body name))
  249.          expression)
  250.         ((and (null? (cdr sids))
  251.           (pair? body)
  252.           (pair? (cdr body))
  253.           (eq? (cadr body) name)
  254.           (null? (cddr body)))
  255.          `(,(rename 'MAP) ,(car body) ,expression))
  256.         (else
  257.          `(,(rename 'MAP) (,(rename 'LAMBDA) ,(map sid-name sids) ,body)
  258.                   ,@(map sid-expression sids)))))))
  259.  
  260. (define (zero-or-more? pattern rename compare)
  261.   (and (pair? pattern)
  262.        (pair? (cdr pattern))
  263.        (identifier? (cadr pattern))
  264.        (compare (cadr pattern) (rename '...))))
  265.  
  266. (define (at-least-one? pattern rename compare)
  267. ;;;  (and (pair? pattern)
  268. ;;;       (pair? (cdr pattern))
  269. ;;;       (identifier? (cadr pattern))
  270. ;;;       (compare (cadr pattern) (rename '+)))
  271.   pattern rename compare        ;ignore
  272.   #f)
  273.  
  274. (define (optimized-cons rename compare a d)
  275.   (cond ((and (pair? d)
  276.           (compare (car d) (rename 'QUOTE))
  277.           (pair? (cdr d))
  278.           (null? (cadr d))
  279.           (null? (cddr d)))
  280.      `(,(rename 'LIST) ,a))
  281.     ((and (pair? d)
  282.           (compare (car d) (rename 'LIST))
  283.           (list? (cdr d)))
  284.      `(,(car d) ,a ,@(cdr d)))
  285.     (else
  286.      `(,(rename 'CONS) ,a ,d))))
  287.  
  288. (define (optimized-append rename compare x y)
  289.   (if (and (pair? y)
  290.        (compare (car y) (rename 'QUOTE))
  291.        (pair? (cdr y))
  292.        (null? (cadr y))
  293.        (null? (cddr y)))
  294.       x
  295.       `(,(rename 'APPEND) ,x ,y)))
  296.  
  297. (define sid-type
  298.   (make-record-type "sid" '(NAME EXPRESSION CONTROL OUTPUT-EXPRESSION)))
  299.  
  300. (define make-sid
  301.   (record-constructor sid-type '(NAME EXPRESSION CONTROL)))
  302.  
  303. (define sid-name
  304.   (record-accessor sid-type 'NAME))
  305.  
  306. (define sid-expression
  307.   (record-accessor sid-type 'EXPRESSION))
  308.  
  309. (define sid-control
  310.   (record-accessor sid-type 'CONTROL))
  311.  
  312. (define sid-output-expression
  313.   (record-accessor sid-type 'OUTPUT-EXPRESSION))
  314.  
  315. (define set-sid-output-expression!
  316.   (record-modifier sid-type 'OUTPUT-EXPRESSION))
  317.  
  318. (define ellipsis-type
  319.   (make-record-type "ellipsis" '(SIDS)))
  320.  
  321. (define make-ellipsis
  322.   (record-constructor ellipsis-type '(SIDS)))
  323.  
  324. (define ellipsis-sids
  325.   (record-accessor ellipsis-type 'SIDS))
  326.  
  327. (define set-ellipsis-sids!
  328.   (record-modifier ellipsis-type 'SIDS))
  329.